package org.example.teacher.Service;

import org.example.teacher.Dao.TeacherDao;
import org.example.teacher.entity.teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService implements TeacherDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(teacher teacher) {
        String sql = "insert into teacher values(null,?,?)";
        Object[] args = {
                teacher.getName(), teacher.getAge(),

        };
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int delete(teacher teacher) {
        String sql="delete from teacher where Id=?";
        Object[] args={teacher.getId()};
        return jdbcTemplate.update(sql, args);
    }


    @Override
    public int update(teacher teacher) {
        String sql="update teacher set name=?,age=? where id=?";
        Object[] args={
                 teacher.getName(), teacher.getAge(),teacher.getId()
        };
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public teacher findone(teacher teacher) {
        String sql = "select * from teacher where Id="+ teacher.getId();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<teacher>(teacher.class));
    }

    @Override
    public List<teacher> findAll() {
        String sql = "select * from teacher";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<teacher>(teacher.class));

    }
}